package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author MissChen
 * @date 2024年05月16日 15:04
 */
@WebServlet("*.product")
public class ProductServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");
       response.setContentType("text/html;charset=utf-8");
       String path=request.getServletPath();
       switch (path){
           case "/findAll.product":
               findAll(request,response);
               break;
       }

    }
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ProductService productService= (ProductService) ObjectFactory.getObject("productService");
        try {
            List<Product> products=productService.findAll();
            request.setAttribute("products",products);
        } catch (Exception e) {
            request.setAttribute("productMsg","服务器内部异常");
        }
        request.getRequestDispatcher("/jsp/main.jsp").forward(request,response);


    }
}
